﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Transactions;

namespace SMS.Client
{
    public partial class NewClientTransaction : System.Web.UI.Page
    {
        SMS.Business.Customer.CustomerBussiness CustBuss;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurrentUser"] == null)
                Response.Redirect(@"~\Account\Login.aspx");
            CustBuss = new Business.Customer.CustomerBussiness();
            if (!IsPostBack)
            {
                this.ddlCustomers.DataSource = CustBuss.GetAll();
                this.ddlCustomers.DataTextField = "Name";
                this.ddlCustomers.DataValueField = "ID";
                this.DataBind();

                SMS.DAL.Customer CST = CustBuss.GetByID(int.Parse(this.ddlCustomers.SelectedValue));
                lblName.Text = CST.Name;
                LoadPageControls(CST);

            }
        }

        protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
        {
            SMS.DAL.Customer CST = CustBuss.GetByID(int.Parse(this.ddlCustomers.SelectedValue));
            lblCrdt.Text = txtPayment.Text = Math.Abs((decimal)CST.Credit).ToString();
            lblName.Text = CST.Name;
            LoadPageControls(CST);
        }

        private void LoadPageControls(DAL.Customer CST)
        {
            if (CST.Credit == 0)
            {
                lblStatus.Text = "لا يوجد / تم تسديد التعاملات";
                this.txtPayment.Visible = false;
                this.btnSave.Visible = false;
                this.Label3.Visible = false;
                lblCrdt.Text = "0";
            }
            else
            {
                lblCrdt.Text = txtPayment.Text = Math.Abs((decimal)CST.Credit).ToString();
                vldrange.MaximumValue = Math.Abs((decimal)CST.Credit).ToString();
                vldrange.MinimumValue = "1";
                vldrange.ErrorMessage = string.Format("الرجاء ادخل مبلغ مابين جنيه و {0} جنيهات", vldrange.MaximumValue);
                vldrange.ForeColor = System.Drawing.Color.Red;
                this.txtPayment.Visible = true;
                this.btnSave.Visible = true;
                this.Label3.Visible = true;

                if (CST.Credit > 0)
                {
                    lblStatus.Text = "دائن";
                }
                else
                {
                    lblStatus.Text = "مدين";
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool Success = false;
            SMS.Business.Customer.CustomerTransactionsBusiness tmp = new Business.Customer.CustomerTransactionsBusiness();
            SMS.DAL.Customer CST = CustBuss.GetByID(int.Parse(this.ddlCustomers.SelectedValue));

            using (TransactionScope transaction = new TransactionScope())
            {
                if (CST.Credit > 0)
                    CST.Credit = CST.Credit - decimal.Parse(txtPayment.Text);
                else
                    CST.Credit = CST.Credit + decimal.Parse(txtPayment.Text);

                CustBuss.Update(CST);
                tmp.Insert(new DAL.CutomerCreditTransaction()
                {
                    CustomerID = int.Parse(ddlCustomers.SelectedValue),
                    Date = DateTime.Now,
                    OrderID = null,
                    Credit = lblStatus.Text == "دائن" ? double.Parse(txtPayment.Text) : 0,
                    Debit = lblStatus.Text == "مدين" ? double.Parse(txtPayment.Text) : 0,
                    UserID = ((SMS.DAL.User)Session["CurrentUser"]).ID
                });

                transaction.Complete();
                Success = true;
            }
            if (Success)
            {
                this.lblCrdt.Text = CST.Credit.ToString();

                if (CST.Credit == 0)
                {
                    lblStatus.Text = "لا يوجد / تم تسديد التعاملات";
                    this.txtPayment.Visible = false;
                    this.btnSave.Visible = false;
                    this.Label3.Visible = false;
                }
                else
                {
                    this.txtPayment.Text = CST.Credit.ToString();
                }
            }
            else
            {

            }


        }
    }
}